<template>
  <div></div>
</template>
<script lang="ts" setup>
  import { unref, onMounted } from 'vue';
  import { useRouter } from 'vue-router';

  const { currentRoute, replace } = useRouter();

  onMounted(() => {
    const { params, query } = unref(currentRoute);
    const { path, _redirect_type = 'path' } = params;

    // 创建新的params对象，避免修改原始对象
    const newParams = { ...params };
    delete newParams._redirect_type;
    delete newParams.path;

    const _path = Array.isArray(path) ? path.join('/') : decodeURIComponent(path);

    console.log('Redirect: type=', _redirect_type, 'path=', _path, 'query=', query, 'params=', newParams);

    try {
      if (_redirect_type === 'name') {
        replace({
          name: _path,
          query,
          params: newParams,
        });
      } else {
        replace({
          path: _path.startsWith('/') ? _path : '/' + _path,
          query,
        });
      }
    } catch (error) {
      console.error('Redirect error:', error);
      // 如果重定向失败，尝试使用基本路径
      replace('/');
    }
  });
</script>
